$(function () {
    $("#save-banner-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#banner-dialog");
        var nameInput = $("input[name='name']");
        var image_urlInput = $("input[name='image_url']");
        var link_urlInput = $("input[name='link_url']");
        var priorityInput = $("input[name='priority']");

        var name = nameInput.val();
        var image_url = image_urlInput.val();
        var link_url = link_urlInput.val();
        var priority = priorityInput.val();
        var submitType = self.attr("data-type");
        var bannerID = self.attr("data-id");

        if(!name || !image_url || !link_url || !priority){
            zlalert.alertInfoToast("请输入正确的轮播图数据");
            return;
        }

        var url = '';
        if(submitType == 'update'){
            url = '/cms/ubanner/'
        }else{
            url = '/cms/abanner/'
        }

        zlajax.post({
           'url' : url,
           'data' : {
               'name': name,
               'image_url': image_url,
               'link_url': link_url,
               'priority': priority,
               'banner_id': bannerID
           },
           'success': function (data) {
               dialog.modal("hide");
               if (data['code'] == 200) {
                   // 重新加载当前页面
                   window.location.reload();
               } else {
                   zlalert.alertInfo(data['message']);
               }
           },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        });
    });
});

$(function () {
    $(".edit-banner-btn").click(function (event) {
       event.preventDefault();
       var self = $(this);
       var dialog = $("#banner-dialog");
       dialog.modal("show");
       var tr = self.parent().parent();
       var name = tr.attr("data-name");
       var image_url = tr.attr("data-image");
       var link_url = tr.attr("data-link");
       var priority = tr.attr("data-priority");
       var nameInput = dialog.find("input[name='name']");
       var image_urlInput = dialog.find("input[name='image_url']");
       var link_urlInput = dialog.find("input[name='link_url']");
       var priorityInput = dialog.find("input[name='priority']");
       var saveBtn = dialog.find("#save-banner-btn");
       nameInput.val(name);
       image_urlInput.val(image_url);
       link_urlInput.val(link_url);
       priorityInput.val(priority);
       saveBtn.attr("data-type","update");
       saveBtn.attr("data-id",tr.attr("data-id"));
    });
});

$(function () {
    $(".delete-banner-tbn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var banner_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':"您确定要删除这个轮播图吗？", 
            'confirmCallback':function () {
               zlajax.post({
                    'url':'/cms/dbanner/',
                   'data':{
                        'banner_id':banner_id,
                   },
                   'success' : function (data) {
                        if (data['code']==200){
                            window.location.reload();
                        }else{
                            zlalert.alertInfo(data['message']);
                        }
                   }
               });
            }
        });
    });
});

$(function () {
    zlqiniu.setUp({
       'domain': 'http://qnsg65684.hd-bkt.clouddn.com/',
        'browse_btn': 'upload-btn',
        'uptoken_url': '/c/uptoken/',
        'success':function (up,file,info) {
            var imageInput = $("input[name='image_url']");
            imageInput.val(file.name);
        }
    });
});